/* model */
import CardInfo, { CardInfoConfig } from '@model/entity/CardInfo'
import { CardInputTypeEnum } from '@model/enum/CardEnum'
import { parse } from '@src/util/lang/object'
import Log from '@src/util/log'
/* util */
import { isObject, isString } from '@src/util/type'

/** 
 * @description 是否为 附加组件 单次类型
*/
function isSingleCardType(card: CardInfo | string) {
  
  const inputType = isString(card) ? card as string : (card as CardInfo)?.inputType
  
  return inputType == CardInputTypeEnum.Single
}

/** 
 * @description 是否为 附加组件 多次类型
*/
function isMultiCardType(card: CardInfo | string) {
  
  const inputType = isString(card) ? card as string : (card as CardInfo)?.inputType
  
  return inputType == CardInputTypeEnum.Multi
}

/** 
 * @description 是否为 连接器类型的附加组件
*/
function isConnectorCard(card: CardInfo): boolean {
  return card?.specialfrom == '连接器'
}

/** 
 * @description 获取附加组件的配置
*/
function getCardConfig<CardType extends CardInfo, ConfigType extends CardInfoConfig>(card: CardType): ConfigType {
  try {
    
    const config = card.config || {}
    
    if (isString(config)) {
      
      const configObj = parse(config as unknown as string) as unknown
      
      return configObj as ConfigType
      
    }
    
    if (isObject(config)) {
      return config as ConfigType
    }
    
    return {} as ConfigType
    
  } catch (error) {
    
    Log.error(error, getCardConfig.name)
    
    return {} as ConfigType
    
  }
}

export {
  isSingleCardType,
  isMultiCardType,
  isConnectorCard,
  getCardConfig
}
